using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Data.Objects;
using DataAccess.BusinessEntities;
using DataAccess.Services;

namespace DataAccess
{
    public class SqlUnitOfWork : IUnitOfWork
    {
        //readonly LcboDrinkLocatorContext context42 = new LcboDrinkLocatorContext();

        public SqlUnitOfWork()
        {
            var connectionString = String.Format("metadata=res://*/LcboDrinkLocator.csdl|res://*/LcboDrinkLocator.ssdl|res://*/LcboDrinkLocator.msl;provider=System.Data.SqlClient;provider connection string='{0}'", ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString);
            this.context = new ObjectContext(connectionString);
            this.context.ContextOptions.LazyLoadingEnabled = false;
            this.context.ContextOptions.ProxyCreationEnabled = true;
            //this.context42.Configuration.LazyLoadingEnabled = false;
        }

        public IRepository<Store> Stores
        {
            get
            {
                if (this.stores == null)
                {
                    this.stores = new SqlRepository<Store>(this.context);
                }

                return stores;
            }
        }

        public IRepository<User> Users
        {
            get
            {
                if (this.users == null)
                {
                    this.users = new SqlRepository<User>(this.context);
                }

                return users;
            }
        }

        public void Commit()
        {
            this.context.SaveChanges();
        }

        private SqlRepository<Container> containers = null;
        private readonly ObjectContext context;
        private SqlRepository<Store> stores;
        private SqlRepository<User> users;
        private const string ConnectionStringName = "LcboDrinkLocatorConnection";//"LcboDrinkLocatorEntities";
        private bool disposed;
        private SqlRepository<PersonalLocation> personalLocations;
        private SqlRepository<Product> products;
        private SqlRepository<Substance> substances;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.context != null)
                    {
                        this.context.Dispose();
                    }

                    //if (this.context42 != null)
                    //{
                    //    this.context42.Dispose();
                    //}
                }
            }

            this.disposed = true;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~SqlUnitOfWork()
        {
            this.Dispose(false);
        }

        public IRepository<PersonalLocation> PersonalLocations
        {
            get
            {
                if (this.personalLocations == null)
                {
                    this.personalLocations = new SqlRepository<PersonalLocation>(this.context);
                }

                return personalLocations;
            }
        }

        public IRepository<Product> Products
        {
            get
            {
                if (this.products == null)
                {
                    this.products = new SqlRepository<Product>(this.context);
                }

                return products;
            }
        }

        public IRepository<Substance> Substances
        {
            get
            {
                if (this.substances == null)
                {
                    this.substances = new SqlRepository<Substance>(this.context);
                }

                return substances;
            }
        }

        public IRepository<Container> Containers
        {
            get
            {
                if (this.containers == null)
                {
                    this.containers = new SqlRepository<Container>(this.context);
                }

                return containers;
            }
        }
    }
}
